import { createRouter, createWebHashHistory } from "vue-router";
import Login from "../page/Login.vue";
import Home from "../page/Home.vue";
import DashBoard from "../page/Child/DashBoard.vue";
import Notfound from "../page/Notfound.vue";
import pinia from "../stores";
import { UserInfoStore } from "../stores/userInfo.js";

const store = UserInfoStore(pinia);

const Router = createRouter({
  history: createWebHashHistory(),
  base: "/",
  routes: [
    { path: "/", redirect: "/home" },
    { path: "/login", component: Login, name: "Login" },
    {
      path: "/home",
      component: Home,
      redirect: "/dash",
      children: [
        { path: "/dash", component: DashBoard },
        {
          path: "/examine",
          component: () => import("../page/Child/Examine.vue"),
        },
        {
          path: "/reject",
          component: () => import("../page/Child/RejectHistory.vue"),
        },
        {
          path: "/history",
          component: () => import("../page/Child/History.vue"),
        },
        {
          path: "/account",
          component: () => import("../page/Child/AccountHandle.vue"),
        },
        {
          path: "/trends",
          component: () => import("../page/Child/Trends.vue"),
        },
        {
          path: "/advertise",
          component: () => import("../page/Child/Advertisement.vue"),
        },
        {
          path: "/platform",
          component: () => import("../page/Child/Platform.vue"),
        },
      ],
    },
    { path: "/NotFound", component: Notfound },
  ],
});


Router.beforeEach((to, from, next) => {
  if (store.loginStatus != true 
    && to.name !== "Login") {
    next({ name: "Login" });
  } else {
    next();
  }
  next();
});

export default Router;
